<?php
/**
 * ClassName:渠道详情对象
 * @auditor:leijunxiang
 * @time 2025/4/2 17:32
 */

namespace app\modelOperate\freightForward;

use app\modelOperate\ModelOpeBase;
use think\facade\Log;

class ChannelDetailOpe extends ModelOpeBase
{
    protected $pk = 'detailId';


    /**
     * 保存数据
     * @param $data
     * @return void
     */
    public function saveData($data)
    {
        $db = $this->getDb();
        $flag = $db->insert($data);
        return $flag;
    }


    /**
     * 根据id查询数据
     * @return mixed
     */
    public function selectDataByChannelId($channelId, $page, $limit)
    {
        $db = $this->getDb();
        $data = $db
            ->where('channelId', '=', $channelId)
            ->page($page, $limit)
            ->order('time', 'desc')
            ->select()
            ->toArray()
        ;
        return $data;
    }


    /**
     * 根据id和区域名称查询数据
     * @return mixed
     */
    public function selectDataByChannelIdAndAreaName($channelId, $areaName, $page, $limit)
    {
        $db = $this->getDb();
        $data = $db
            ->where('channelId', '=', $channelId)
            ->where('areaName', 'like', '%' . $areaName . '%')
            ->page($page, $limit)
            ->order('time', 'desc')
            ->select()
            ->toArray();
        return $data;
    }

    /**
     * 根据id查询数据
     * @return mixed
     */
    public function selectDetailDataByChannelId($channelId,$warehouseName)
    {
        $db = $this->getDb();
        $data = $db
            ->where('channelId', '=', $channelId)
            ->where('areaName', '=', $warehouseName)
            ->select()
            ->toArray();
        return $data;
    }


    /**
     * 根据区域名称查询id
     * @param $areaName
     * @return string
     */
    public function selectIdByAreaName($areaName,$channelId)
    {
        $db = $this->getDb();
        $arr = $db
            ->where('areaName', '=', $areaName)
            ->where('channelId', '=', $channelId)
            ->field('detailId')
            ->find();
        $id = $arr['detailId'];
        return $id;
    }

    /**
     * 根据渠道id查询数量
     * @param $channelId
     * @return mixed
     */
    public function selectCount($channelId)
    {
        $db = $this->getDb();
        $count = $db
            ->where('channelId', '=', $channelId)
            ->count();
        return $count;
    }

    /**
     * 根据渠道id和名称查询数量
     * @param $channelId
     * @return mixed
     */
    public function selectCountBayName($channelId,$areaName)
    {
        $db = $this->getDb();
        $count = $db
            ->where('channelId', '=', $channelId)
            ->where('areaName', 'like', '%' . $areaName . '%')
            ->count();
        return $count;
    }

    /**
     * 修改详情
     */
    public function updateDetailData($data)
    {
        $db = $this->getDb();
        $arr = [
            'areaName' => $data['areaName'],
        ];

        $flag = $db->where('detailId', '=', $data['detailId'])->update($arr);
        return $flag;
    }

    /**
     * 删除
     */
    public function deleteDetailData($detailId)
    {
        $db = $this->getDb();
        $flag = $db->where('detailId', '=', $detailId)->delete();
        return $flag;
    }
}